﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using Contacts.Data.Data;
using Contacts.Data.Model;

namespace Contacts.Web
{
   public partial class PersonList : BasePage
   {
      private readonly ContactsDataPersons _contactsDataPersons = new ContactsDataPersons();
      private ContactsDataGroups _dataGroups = new ContactsDataGroups();

      protected override void PageLoad()
      {
         if (!IsPostBack)
         {
            ReShowPersonList();

            ddSearchGroup.DataSource = _dataGroups.LoadAllGroups();
            ddSearchGroup.DataTextField = "GroupName";
            ddSearchGroup.DataValueField = "GroupID";
            ddSearchGroup.DataBind();
         }

      }


      public void personsView_OnRowCommand(object sender, GridViewCommandEventArgs e)
      {
         ////TODO Проверка пути иконки и отлов аргументов:

         string i = e.CommandArgument.ToString();
         int result;
         if (int.TryParse(i, out result)) return;
         if (i == "Next") return;
         if (i == "Prev") return;
         if (i == "First") return;
         if (i == "Last") return;

         string[] splitArgs = e.CommandArgument.ToString().Split('|');
         string personID = splitArgs[0];
         string photoName = splitArgs[1];

         string commandName = e.CommandName;

         if (commandName == "DeleteCmd")
         {
            if (photoName != "Default.png")
            {
               File.Delete("C:\\Images\\" + photoName);
            }

            _contactsDataPersons.DeletePerson(Convert.ToInt32(personID));
            ReShowPersonList();
         }
         else if (commandName == "EditCmd")
         {
            Response.Redirect("PersonEditor.aspx?PersonID=" + personID);
         }
      }

      private string FormatPhone(string num)
      {
         num = Regex.Replace(num, @"[^\d]", "");
         string formatPattern = @"(\d{3})(\d{3})(\d{2})(\d{2})";
         return Regex.Replace(num, formatPattern, "($1) $2-$3-$4");
      }

      public void ReShowPersonList()
      {

         List<Persons> persons = _contactsDataPersons.LoadAllPersonsAndGroups();
         persons.ForEach(one => one.Phone = FormatPhone(one.Phone));
         personsView.DataSource = persons;
         personsView.DataBind();


         // у GridView OnRowDataBound
      }

      protected void BtnSearchClick(object sender, EventArgs e)
      {
         List<Persons> persons = _contactsDataPersons.SearchPersonsOfGroup(txtSearch.Text, Convert.ToInt32(ddSearchGroup.SelectedValue));
         persons.ForEach(one => one.Phone = FormatPhone(one.Phone));
         personsView.DataSource = persons;
         personsView.DataBind();
      }

      protected void personsView_RowDataBound(object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
            DateTime date = DateTime.ParseExact(e.Row.Cells[5].Text, "dd MMM yyyy", null);
            if (date == new DateTime(1900, 1, 1))
            {
               e.Row.Cells[5].Text = "";
            }
         }
      }

      protected void personsView_PageIndexChanging(object sender, GridViewPageEventArgs e)
      {
         if (e.NewPageIndex < 0)
         {
            ReShowPersonList();
         }
         else
         {
            personsView.PageIndex = e.NewPageIndex;
            ReShowPersonList();
         }

      }












   }
}